package com.wuzf.mall.config

import android.os.Handler
import android.os.Looper
import com.wuzf.mall.global.GlobalKeys
import com.wuzf.mall.util.MemoryStore

class Configuration private constructor() {

    private object Holder {
        val holder = Configuration()
    }

    companion object {
        val store = MemoryStore.instance
        val instance = Holder.holder
    }

    init {
        store.addData(GlobalKeys.IS_CONFIGURE_READY, false)
        // 添加handler
        store.addData(GlobalKeys.HANDLER, Handler(Looper.getMainLooper()))
    }

    fun withApiHost(host: String): Configuration {
        store.addData(GlobalKeys.API_HOST, host)
        return this;
    }

    fun withDelayTime(time: Long): Configuration {
        store.addData(GlobalKeys.DELAY_TIME, time)
        return this
    }

    /**
     * 结束一些配置
     */
    fun configure() {
        store.addData(GlobalKeys.IS_CONFIGURE_READY, true)
    }

    fun <T> getConfiguration(key: String): T {
        return store.getData<T>(key)
    }

    fun <T> getConfiguration(key: Enum<*>): T {
        return store.getData<T>(key)
    }

    fun setConfiguration(key: String, value: Any) {
        store.addData(key, value)
    }

    fun setConfiguration(key: Enum<*>, value: Any) {
        store.addData(key, value)
    }
}